import argparse

def load_tsv(file_path):
    data = {}
    with open(file_path, 'r') as file:
        for line in file:
            parts = line.strip().split('\t')
            if int(parts[9])<5:
                continue
            if parts[1]=='-1':
                continue
            key = (parts[0], parts[1])
            value = float(parts[10])
            data[key] = value
    return data

def find_matching_keys(file1, file2, file3):
    matching_keys = set()
    
    for key, value1 in file1.items():
        if value1 == 10:
            if key in file2 and file2[key] < 50:
                if key in file3 and file3[key] > 50:
                    matching_keys.add(key)
    
    return matching_keys

def compare_files(file3, file4, matching_keys):
    differences = []
    
    for key in matching_keys:
        if key in file4:
            if file3[key] != file4[key]:
                differences.append((key, file3[key], file4[key]))
    
    return differences

def main():
    parser = argparse.ArgumentParser(description="Compare TSV files based on specific conditions.")
    parser.add_argument('file1', type=str, help='Path to the first TSV file, bs')
    parser.add_argument('file2', type=str, help='Path to the second TSV file, dorado')
    parser.add_argument('file3', type=str, help='Path to the third TSV file')
    parser.add_argument('file4', type=str, help='Path to the fourth TSV file')
    
    args = parser.parse_args()

    # Load data from files
    file1_data = load_tsv(args.file1)
    file2_data = load_tsv(args.file2)
    file3_data = load_tsv(args.file3)
    file4_data = load_tsv(args.file4)

    # Find matching keys
    matching_keys = find_matching_keys(file1_data, file2_data, file3_data)

    # Compare values in file3 and file4 for matching keys
    differences = compare_files(file3_data, file4_data, matching_keys)

    # Output differences
    for key, value3, value4 in differences:
        print(f"Key: {key}, File 3 Value: {value3}, File 4 Value: {value4}")

if __name__ == "__main__":
    main()
